﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ProcessoSeletivo.DAO
{
    public class Processo : IDisposable
    {
        #region " Listar "

        public DataView ListarProcesso()
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_processo");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ListarProcessoDescricao()
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarProcessoDescricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_processo");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ListarProcessoTipo()
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarProcessoTipo", sql);
                sqlc.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_processo");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Consultar "

        public DataView ConsultarProcesso(int cod_processo_pro)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_processo");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ConsultarProcessoPorDescricao(string des_processo_pro)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarProcessoPorDescricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@des_processo_pro", des_processo_pro);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_processo");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Incluir "

        public int IncluirProcesso(DataSet dsRegistro)
        {
            int PrimaryKey = 0;

            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("IncluirProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@des_processo_pro", dsRegistro.Tables[0].Rows[0]["des_processo_pro"]);
                sqlc.Parameters.AddWithValue("@tip_processo_pro", dsRegistro.Tables[0].Rows[0]["tip_processo_pro"]);

                PrimaryKey = Convert.ToInt32(sqlc.ExecuteScalar().ToString());
                sql.Close();
            }

            return PrimaryKey;
        }

        #endregion

        #region " Alterar "

        public void AlterarProcesso(DataSet dsRegistro)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("AlterarProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@des_processo_pro", dsRegistro.Tables[0].Rows[0]["des_processo_pro"]);
                sqlc.Parameters.AddWithValue("@tip_processo_pro", dsRegistro.Tables[0].Rows[0]["tip_processo_pro"]);
                sqlc.Parameters.AddWithValue("@cod_processo_pro", dsRegistro.Tables[0].Rows[0]["cod_processo_pro"]);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Excluir "

        public void ExcluirProcesso(int cod_processo_pro)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ExcluirProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Dipose "

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
